package com.hdoit.ares.web.ser.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.ser.model.GoodStore;
import com.hdoit.ares.logical.ser.service.IGoodStoreService;
import com.hdoit.ares.logical.ser.service.IStaffService;
import com.hdoit.ares.logical.viewmodel.ser.GoodStoreSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      lvsi
 * @email       
 * @createtime  2017年11月25日 16:00:07
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/ser/goodStore/")
public class GoodStoreController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/ser/goodStore/";
    }

    private IGoodStoreService goodStoreService;
    public IGoodStoreService getGoodStoreService() {
        return goodStoreService;
    }
    @Resource
    public void setGoodStoreService(IGoodStoreService goodStoreService) {
        this.goodStoreService = goodStoreService;
    }
    private IStaffService staffService;

   	public IStaffService getStaffService() {
   		return staffService;
   	}
   	@Resource
   	public void setStaffService(IStaffService staffService) {
   		this.staffService = staffService;
   	}
   	
   	private IRoleEmployeeService roleEmployeeService;

    public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}
    @Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}

	private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="goodStoreDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
  
    
    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,GoodStoreSearch goodStoreSearch,boolean ajaxPost){
    	
    	String loginId=getLoginModel().getLoginId();
    	boolean result=roleEmployeeService.queryCountRolesByEmployeeId("超级管理员",loginId);
    	
    	List<GoodStore> goodStores=null;
    	if(goodStoreService.queryListStoreById(loginId).size()==0 && staffService.queryStaffById(loginId).size()==0 
    			&& result==true){
    		pagerModel.setTotalAmount(goodStoreService.queryAllStoreCount(goodStoreSearch));
            goodStores= goodStoreService.queryAllStore(goodStoreSearch,pagerModel);
    	}else{
    		pagerModel.setTotalAmount(goodStoreService.queryCountMPById(goodStoreSearch,loginId));
            goodStores= goodStoreService.queryListMPById(goodStoreSearch,pagerModel, loginId);
    	}

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("goodStores", goodStores);
        model.addAttribute("goodStoreSearch", goodStoreSearch);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String goodStoreDetailed(Model model,String key){
    
        GoodStore goodStore=new GoodStore();
        if(!StringUtils.isEmpty(key)){
            goodStore=goodStoreService.queryModel(key);
        }else if(StringUtils.isEmpty(key)){
            goodStore=new GoodStore();
            goodStore.setDeleteMark(YesOrNo.no);
        	goodStore.setEmployeeId(getLoginModel().getLoginId());
            goodStore.setStoreKeeperName(getLoginModel().getLoginName());
        }
        model.addAttribute("goodStore", goodStore);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,GoodStore goodStore){
        if(StringUtils.isEmpty(goodStore.getId())){
            goodStore.setId(CommonUtils.getKeyId());
            goodStore.setDeleteMark(YesOrNo.no);
            goodStore.setEmployeeId(getLoginModel().getLoginId());
            goodStore.setStoreKeeperName(getLoginModel().getLoginName());
            goodStore.setModelStatus(ModelStatus.add);
        }else{
            goodStore.setEmployeeId(getLoginModel().getLoginId());
            goodStore.setStoreKeeperName(getLoginModel().getLoginName());
            goodStore.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(goodStoreService.applyChange(goodStore)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("goodStore", goodStore);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(goodStoreService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}